#!/usr/bin/env bash
# © 2006-2011 Matt Sicker
# This program is Free Software; you may copy, modify, and distribute it under
# the terms of the GNU General Public License version 3, or (at your option)
# any later version published by the Free Software Foundation.
# If you did not receive a copy of the GPL with this distribution, you can
# download the latest version at <http://www.gnu.org/licenses/gpl.txt> or write
# to:
# Free Software Foundation, Inc.,
# 51 Franklin St - Fifth Floor,
# Boston, MA 02110-1301 USA
#
# BLAH BLAH WARRANTY:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

PS_CMD="$(which ps)"
PS_OPTS="ux"
GREP_CMD="$(which grep)"
GREP_OPTS=""

PSGREP_VERSION="trunk"

usage()
{
    echo "Usage: $0 [options] <search terms>

Options:
    -n          Don't print header
    -a          Search processes of all users
    -b          Search using BSD's ps format (ux) [default]
    -l          Search using Linux's ps format (-ef)
    -s          Search using simplified format (PID, user, command)
    -o <opts>   Search using custom ps output format
    -d          Search using PID-only format (similar to pgrep)
    -i          Ignore case in searches
    -e          Use extended regexps (egrep)
    -p          Use perl regexps (grep -P)
    -h          View this message
    -v          View version information"
    exit 0
}

version_info()
{
    echo "psgrep version ${PSGREP_VERSION}
© 2006-2011 Matt Sicker
This program comes with ABSOLUTELY NO WARRANTY; for details, see the included
COPYING file.  This is free software, and you are welcome to redistribute it
under certain conditions; see the included COPYING file for details.  In short,
you may copy, modify, and re/distribute it under the terms of the GNU General
Public License version 3, or (at your option) any later version."
    exit 2
}

[ $# -lt 1 ] && usage

PID_ONLY=0
while getopts "nablso:iephvd" opt; do
    case $opt in
        h) usage;;
        v) version_info;;
        n) NO_HEADER=1;;
        a) PS_OPTS="aux";;
        b) PS_OPTS="ux";;
        l) PS_OPTS="-ef";;
        s) PS_OPTS="-eo pid,euser,comm";;
        o) PS_OPTS="-eo $OPTARG";;
        i) GREP_CMD="${GREP_CMD} -i";;
        e) GREP_CMD="${GREP_CMD} -E";;
        p) GREP_CMD="${GREP_CMD} -P";;
        d) PS_OPTS="-eo pid,comm";
           NO_HEADER=1;
           PID_ONLY=1;;
    esac
done

# now the actual execution
HEADER=$($PS_CMD $PS_OPTS | head -n1)
RESULT=$($PS_CMD $PS_OPTS | $GREP_CMD -- "${*:$OPTIND}" | $GREP_CMD -v "$GREP_CMD" | $GREP_CMD -v "$0" | awk '{print $'$PID_ONLY' }')
if [ "$RESULT" ]; then
    [ ! "$NO_HEADER" ] && echo "$HEADER"
    echo "$RESULT"
fi
